package com;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MainClientThread extends PublicThread {

	private boolean isConnect = false;
	private ServerThread server = null;
	private String address = "";
	private int port = 0;

	public MainClientThread(Socket socket, ThreadPool pool) {
		this.socket = socket;
		this.threadPool = pool;
		addToThreadPool();
	}

	public void run() {
		InputStream in = null;
		byte[] data = new byte[4096];
		int lenght = 0;
		try {
			in = socket.getInputStream();
			String str = "";
			String[] inStr = null;
			while (isRun && lenght > -1) {
				lenght = in.read(data);
				if (lenght > 0) {
					str = new String(data, 0, lenght).trim();
					System.out.println("new request data  " + str);
					inStr = str.split(" ");
					if (inStr != null) {

						if (inStr[0].equals("Info")) {
							this.address = getParam(inStr, "Address");
							try {
								this.port = Integer.parseInt(getParam(inStr, "Port"));
							
							} catch (Exception e) {
								System.out.println(e);
							}
						} else if (inStr[0].equals("NewPort")) {
							String[] temp = str.split(",");
							for (String item : temp) {
								if(item == null)
									continue;
								
								inStr = item.split(" ");
								int port = 0;
								try {
									port = Integer.parseInt(inStr[1]);
								} catch (Exception e) {
									System.out.println(e);
									continue;
								}
					
								try {
								//	ClientThread client = new ClientThread(new Socket(this.address, this.port), this.threadPool);
									ClientThread client = new ClientThread(new Socket("127.0.0.1", this.port), this.threadPool);
									System.out.println("new client address:" + this.address + "   prot" + this.port );
									
									try {
										String address = socket.getInetAddress().getHostAddress();
										ServerThread server = new ServerThread(new Socket(address, port), this.threadPool);
										System.out.println("new servser address:" + address + "   prot" + this.port );
										
										client.setServer(server);
										server.setClient(client);

										client.newThreadStrat();
										server.newThreadStrat();
									} catch (Exception e) {
										client.close();
										System.out.println("Socket Error " + e.getMessage() + "  Address: " + this.address + " Port:" + this.port);
									}
								} catch (Exception e) {
									System.out.println("Socket Error " + e.getMessage() + "  Address: " + this.address + " Port:" + this.port);
								}
							}
						}
					}
				}
				sleep(10);
			}
			System.out.println("Remote Server Close, Address: " + socket.getInetAddress().getHostAddress() + ", Port: " + socket.getPort());
		} catch (

		IOException e)

		{
			System.out.println("ServerThread Error " + e.getMessage());
		}
		socket = null;
		isConnect = false;

		close();

		removeFromThreadPool();

	}

	public void close() {
		synchronized (this) {
			try {
				super.closeThread();
			} catch (Exception e1) {
				System.out.print("close MainClinetThread socket Error, Port:" + serverSocket.getLocalPort());
			}
		}
	}

	public void serServer(ServerThread server) {
		this.server = server;
	}

	public boolean isConnect() {
		return this.isConnect;
	}

	public void send(byte[] bytes) {
		synchronized (this) {
			if (socket != null) {
				try {
					socket.getOutputStream().write(bytes);
				} catch (IOException e) {
					System.out.println("MainClinetThread Send Data Error, Address: " + socket.getInetAddress().getHostAddress() + ", Port: " + socket.getPort());
				}
			}
		}
	}

	private String getParam(String[] args, String key) {
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals(key)) {
				return args[i + 1].trim();
			}
		}
		return "";
	}
}
